<?php

namespace app\index\service;

use app\common\model\Consulting;
use app\common\model\ConsultingComment;
use app\common\model\ConsultingQuestion;
use app\common\model\ConsultingServer;
use app\common\model\Trade;
use app\index\dao\RankBroadcastDao;
use app\index\model\UM as UM;
use ShidianUtil\Utils\TimeUtil;

/**
 * Class HotRankInfoService
 * @package app\index\service
 */
class HotRankInfoService
{
    // dao
    private RankBroadcastDao $rankBroadcastDao;

    public function __construct(RankBroadcastDao $rankBroadcastDao)
    {
        $this->rankBroadcastDao = $rankBroadcastDao;
    }

    /**
     * 查询
     * @param UM|null $user
     * @return array
     */
    public function read(?UM $user): array
    {
        $time = TimeUtil::stamp(TimeUtil::DAY);

        // 付费订单
        $ffdd = Trade::where('payStatus', Trade::PAID)
            ->where('tradeType', TRADE_CONSULTING_SERVER)
            ->where('payTime', '>=', $time)
            ->count();

        // 案源留言
        $ayly = ConsultingQuestion::where('created', '>=', $time)->count();

        // 咨询回复数
        $zxhfs = Consulting::where('isAnswered', 1)
            ->where('created', '>=', $time)
            ->count();

        // 付费追问数
        $ffzws = 0;

        // 1v1咨询订单
        $phoneConsultingServerIds = ConsultingServer::where('serverType', ConsultingServer::PHONE)
            ->field(['id'])
            ->select()
            ->column('id');
        $zxdd1v1 = Trade::where('payStatus', Trade::PAID)
            ->where('tradeType', TRADE_CONSULTING_SERVER)
            ->where('payTime', '>=', $time)
            ->when(!empty($phoneConsultingServerIds), fn($query) => $query->whereIn('serverId', $phoneConsultingServerIds))
            ->count();

        // 平台咨询订单
        $ptzxdd = Consulting::where('created', '>=', $time)->count();

        // 佣金返现
        $yjfx = Trade::where('payStatus', Trade::PAID)
            ->where('tradeType', TRADE_CONSULTING_SERVER)
            ->where('payTime', '>=', $time)
            ->sum('lawyerMoney');
        if(!is_numeric($yjfx)){
            $yjfx = 0;
        }

        // 好评数
        $hps = ConsultingComment::where('created', '>=', $time)
            ->where('level','>=', 4)
            ->count('DISTINCT memberId');

        // 差评数
        $cps = ConsultingComment::where('created', '>=', $time)
            ->where('level','<', 4)
            ->count('DISTINCT memberId');

        // 平均接单时间
        $pjjdsj = Consulting::where('rushTime', '>=', $time)
            ->where('rushStatus', 2)
            ->avg('responseTimeLength');
        if(!is_numeric($pjjdsj)){
            $pjjdsj = 0;
        }
        $pjjdsj = round($pjjdsj, 0);

        return [
            'ffdd' => $ffdd,
            'ayly' => $ayly,
            'zxhfs' => $zxhfs,
            'ffzws' => $ffzws,
            'zxdd1v1' => $zxdd1v1,
            'ptzxdd' => $ptzxdd,
            'yjfx' => $yjfx,
            'hps' => $hps,
            'cps' => $cps,
            'pjjdsj' => $pjjdsj,
        ];
    }


}
